import React from 'react';
import Loadable from "react-loadable";
import {Spin} from 'antd';

/**
 * 动态加载的组件要再此显示申明,否则打包时文件会出现未分割导致无法访问的问题
 */
const ComponentList = new Map();
const routes=[];

const componentLoding=<Spin spinning={true}/>;

ComponentList.set("../pages/menu/MenuManagement", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/menu/MenuManagement'),
        loading: loading
    });
    return component;
});
routes.push({path: '/menu/MenuManagement', component: '../pages/menu/MenuManagement', exact: true});

ComponentList.set("../pages/role/Role", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/role/Role'),
        loading: loading
    });
    return component;
});
routes.push({path: '/role/Role', component: '../pages/role/Role', exact: true});

ComponentList.set("../pages/department/Department", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/department/Department'),
        loading: loading
    });
    return component;
});
routes.push({path: '/department/Department', component: '../pages/department/Department', exact: true});

ComponentList.set("../pages/user/User", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/user/User'),
        loading: loading
    });
    return component;
});
routes.push({path: '/user/User', component: '../pages/user/User', exact: true});

ComponentList.set("../pages/header/EditUserInfo", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/header/EditUserInfo'),
        loading: loading
    });
    return component;
});
routes.push({path: '/header/EditUserInfo', component: '../pages/header/EditUserInfo', exact: true});

ComponentList.set("../pages/log/Log", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/log/Log'),
        loading: loading
    });
    return component;
});
routes.push({path: '/log/Log', component: '../pages/log/Log', exact: true});

ComponentList.set("../pages/demo/DataGridDemo", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/demo/DataGridDemo'),
        loading: loading
    });
    return component;
});
routes.push({path: '/demo/DataGridDemo', component: '../pages/demo/DataGridDemo', exact: true});

ComponentList.set("../pages/report/designer/ReportDesigner", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/report/designer/ReportDesigner'),
        loading: loading
    });
    return component;
});
routes.push({path: '/report/designer/ReportDesigner', component: '../pages/report/designer/ReportDesigner', exact: true});

ComponentList.set("../pages/report/datasource/DataSource", (loading) => {
    let component=Loadable({
        loader: () => import('../pages/report/datasource/DataSource'),
        loading: loading
    });
    return component;
});
routes.push({path: '/report/datasource/DataSource', component: '../pages/report/datasource/DataSource', exact: true});

export default class ComponentUtil {
    /**
     * 动态加载组件
     * @param componentName
     * @param loading
     * @returns {*}
     */
    static loadComponent(componentName, loading) {
        let component = null;
        if (componentName) {
            ComponentList.forEach((value, key) => {
                if (key === componentName) {
                    component = value(loading == null ? ComponentUtil.modelLoading : loading);
                }
            });
        }
        return component;
    }

    static hasComponent(componentName){
        return ComponentList.has(componentName);
    }

    static modelLoading() {
        return <Spin spinning={true}/>
    }

    static getAllComponentRoutes(){
        return routes;
    }
}